/*
 * Copyright (c) 2022 Kirky
 * Licensed under the Open Software Licence, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/osl-3.0.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lingeringdream.luciole.helper;

import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.io.IOException;

/**
 * Class that gets data from resource files
 *
 * @author Kirky
 * @version 1.2
 * @date 2021/11/27
 */
public class ResourceHelper {
    private final static HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x0, ResourceHelper.class.getSimpleName());

    public static int getColorInt(Context context, int id, int defaultValue) {
        int color = defaultValue;
        try {
            color = context.getResourceManager().getElement(id).getColor();
        } catch (IOException | NotExistException | WrongTypeException e) {
            HiLog.error(TAG, "class: " + context.getAbilityInfo().getClassName() + "\nerror: " + e.getLocalizedMessage());
        }
        return color;
    }

    public static Color getColor(Context context, int id, int defaultValue) {
        return new Color(getColorInt(context, id, defaultValue));
    }

    public static float getFloat(Context context, int id, float defaultValue) {
        float value = defaultValue;
        try {
            value = context.getResourceManager().getElement(id).getFloat();
        } catch (IOException | NotExistException | WrongTypeException e) {
            HiLog.error(TAG, "class: " + context.getAbilityInfo().getClassName() + "\nerror: " + e.getLocalizedMessage());
        }
        return value;
    }

    public static String getString(Context context, int id, String defaultValue, Object var) {
        String value = defaultValue;
        try {
            if (var != null) {
                value = context.getResourceManager().getElement(id).getString(var);
            } else {
                value = context.getResourceManager().getElement(id).getString();
            }
        } catch (IOException | NotExistException | WrongTypeException e) {
            HiLog.error(TAG, "class: " + context.getAbilityInfo().getClassName() + "\nerror: " + e.getLocalizedMessage());
        }
        return value;
    }

}